package dao.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import dao.UpKeepRecordDao;
import po.car.UpKeepRecord;

public class UpKeepRecordDaoImpl implements UpKeepRecordDao{
	private List<UpKeepRecord> UpKeepRecords;

	public UpKeepRecordDaoImpl() {
		UpKeepRecords = new ArrayList<>();
	}

	@Override
	public boolean insert(UpKeepRecord t) {
		return UpKeepRecords.add(t);
	}

	@Override
	public boolean update(UpKeepRecord t) {
		int index = UpKeepRecords.indexOf(t);
		if(index == -1) {
			return false;
		}else {
			UpKeepRecords.set(index, t);
			return true;
		}
	}

	@Override
	public boolean delete(int id) {
		for(UpKeepRecord ukr : UpKeepRecords) {
			if(ukr.getKeepId() == id) {
				UpKeepRecords.remove(id);
				return true;
			}
		}
		return false;
	}

	@Override
	public UpKeepRecord selectById(int id) {
		Iterator<UpKeepRecord> it = UpKeepRecords.iterator();
		while(it.hasNext()) {
			UpKeepRecord ukr = it.next();
			if(ukr.getKeepId() == id) {
				return ukr;
			}
		}
		return null;
	}

	@Override
	public List<UpKeepRecord> selectAll() {
		List<UpKeepRecord> copy = new ArrayList<>(UpKeepRecords);
		return copy;
	}
	
}
